package com.me.myFramework.aop22.aop120;

import com.me.myFramework.aop22.IBuy;
import com.me.myFramework.aop22.Switch;
import com.me.myFramework.aop22.aop112.intercept.method.MethodInterceptor112;
import com.me.myFramework.aop22.aop120.intercept.method.MethodInvocation120;
import com.me.myFramework.aop22.aop120.intercept.method.ReflectiveMethodInvocation120;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * 《AOP-120》：  <br><br>
 *
 * TODO 以下变更点：<ol>
 *     <li>
 *          {@link MethodInterceptor112#invoke(Method, Object[], Object) } 的参数太散了，统一封装成 {@link ReflectiveMethodInvocation120} 。
 *     </li>
 *     <li>
 *          不重要的修改：为了和 Spring 代码一致，也提供了与 Spring 类似的 {@link MethodInvocation120}接口（先忽略 proceed()、getStaticPart()）。
 *     </li>
 *
 * @author ME
 */
public class MyAop120 {

    public static void main(String[] args) throws Throwable {
        IBuy target = new Switch();

        IBuy proxy = (IBuy) new ProxyFactory120(target, new MethodBeforeAdvice() {
            @Override
            public void before(Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP] 就想看看谁买东西了：" + args[0]);
            }
        }).getProxy();
        proxy.buy("Jack");

        System.out.println("\n----------------------------------------------");
        proxy = (IBuy) new ProxyFactory120(target, new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                System.out.println("[AOP2] 记录购买价格 " + returnValue);
            }
        }).getProxy();
        proxy.buy("Tom");
    }

}
